package com.zzq.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzq.annotation.EnableMyPage;
import com.zzq.domain.dto.SmileCouponDTO;
import com.zzq.domain.entity.SmileClient;
import com.zzq.domain.entity.SmileCoupon;
import com.zzq.domain.search.CouponSearchParamet;
import com.zzq.enumerate.CouponStatusEnum;
import com.zzq.result.SmileResult;
import com.zzq.service.CouponAdminService;
import com.zzq.service.SmileClientService;
import com.zzq.service.SmileCouponService;
import com.zzq.util.CommonUtils;
import com.zzq.util.ObjectUtils;
import com.zzq.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CouponAdminServiceImpl implements CouponAdminService {

    @Autowired
    SmileCouponService couponService;
    @Autowired
    SmileClientService clientService;
    @Override
    @EnableMyPage
    public SmileResult getcouponDTOList(CouponSearchParamet searchParamet) {
        List<SmileCouponDTO> couponDTOList = couponService.getCouponDTOList(searchParamet);
        return SmileResult.sucess("成功",couponDTOList);
    }

    @Override
    @Transactional
    public SmileResult addCoupon(SmileCoupon coupon) {
        SmileClient client = clientService.getClientInfoByClientNo(coupon.getClientNo());
        Long clientId = client.getId();
        couponService.addCoupon(clientId,coupon.getClientNo(),coupon.getCouponRuleId(),coupon.getExpirationTime());
        return SmileResult.sucess("成功");
    }

    @Override
    @Transactional
    public SmileResult updCouponStatus(String couponStatus, Long couponId) {
        if (!ObjectUtils.isEmptys(couponId,couponStatus)){
            LambdaUpdateWrapper<SmileCoupon> wrapper=new LambdaUpdateWrapper();
            wrapper.set(SmileCoupon::getCouponStatus,couponStatus);
            wrapper.eq(SmileCoupon::getId,couponId);
            couponService.update(wrapper);
            return SmileResult.sucess("更新成功");
        }
        return SmileResult.error("数据异常");
    }
}
